package com.banyuan.dao;

import com.banyuan.domain.Author;
import com.banyuan.utils.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;

public class AuthorDao {




    // 通过id 修改密码
    public static int updatePasswordById(Integer id,String password) throws SQLException {
        int i = JDBCUtils.executeUpdate("update author set password=? where id=?", password, id);
        return i;
    }


    // 通过id去查出这个用户的所有信息
    public static ResultSet selectAuthorById(Integer id) throws SQLException {
        ResultSet resultSet = JDBCUtils.executeQuery("select * from author where id = ?", id);
        return resultSet;
    }



    // 通过用户名和密码来查询一个人的所有信息
    public static ResultSet selectAuthorByUserNameAndPassword(String userName, String password) throws SQLException {
        ResultSet resultSet = JDBCUtils.executeQuery("select * from author where userName=? and password=?", userName, password);
        return resultSet;
    }



    // 存储一个用户到数据库

    public static int insert(Author author) throws SQLException {
        int i = JDBCUtils.executeUpdate("insert into author(userName,password,nickName,createTime,updateTime) values(?,?,?,now(),now())",
                author.getUserName(), author.getPassword(), author.getNickName());
        return i;
    }




    // 通过用户名去查询这个用户，
    public static ResultSet selectByUserName(String userName) throws SQLException {
        ResultSet resultSet = JDBCUtils.executeQuery("select count(*) as countSum from author where userName=?", userName);
        return resultSet;
    }


    //  通过传入一个author对象，来修改数据库的值
    public static int updateAuthorById(Author author) throws SQLException {
        int i = JDBCUtils.executeUpdate("update author set nickName=?,updateTime=now() where id = ?",
                author.getNickName(),author.getId());
        return i;
    }





}
